<?php

class UserController extends My_Controller_Action {

    public function loginAction() {
        $this->setContentTitle(_('XXX_USER_LOGIN_PAGE_TITLE'));

        $form = new Application_Form_User_Login();

        if (!$this->isPost() || !$form->isValid($_POST)) {
            $this->view->form = $form;
            return;
        }

        $data = $form->getValues();
        $email = $data['email'];
        $pass = $data['pass'];
        $daoUser = new Application_Model_DAO_User();
        $user = $daoUser->login($email, $pass);
        if (empty($user)) {
            $this->addError($this->_('XXX_USER_LOGIN_INVALID_LOGIN_DATA'));
            $this->view->form = $form;
            $this->setPrintCurrentFlashMessages();
            return;
        }
        $this->addSuccess($this->_('XXX_USER_LOGIN_SUCCESS'));

        $url = $this->getURLWhileLogin();
        if (!$url) {
            $url = $this->url(array('controller' => 'index', 'action' => 'index'));
        }
        $this->_redirect($url);
    }

    public function profileAction() {
        $this->_forward('user-offers');
    }

    public function changePasswordAction() {
        $this->userLoginRequired();
        $this->setContentTitle(_('XXX_USER_PROFILE_CHANGE_PASSWORD_TITLE'));
        $form = new Application_Form_User_ChangePassword();
        if (!$this->isPost() || !$form->isValid($_POST)) {
            $this->view->form = $form;
            $this->render('profile');
            return;
        }
        $data = $form->getValues();

        $daoUser = new Application_Model_DAO_User();
        $user = $daoUser->changePassword($this->userId, $data);
        
        $this->addSuccess($this->_('XXX_CHANGE_PASSWORD_UPDATE_SUCCESS'));                     
        $this->_redirect($this->url(array('controller' => 'user', 'action' => 'change-password')));
    }

    public function contactDataAction() {
        $this->userLoginRequired();
        $this->setContentTitle(_('XXX_USER_PROFILE_CONTACT_DATA_TITLE'));
        $form = new Application_Form_User_ContactData();
            $this->view->form = $form;
        if (!$this->_request->isPost()) {
            $form->populate($this->user);
            $this->render('profile');
            return;
        }
        if (!$form->isValid($_POST)) {
            $this->render('profile');
            return;
        }
        $data = $form->getValues();
        $daoUser = new Application_Model_DAO_User();
        $daoUser->update($this->userId, $data);
        $this->addSuccess($this->_('XXX_CONTACT_DATA_UPDATE_SUCCESS'));
        $this->_redirect($this->url(array('controller' => 'user', 'action' => 'contact-data')));
    }

    public function userOffersAction() {
        $this->userLoginRequired();
        $this->setContentTitle(_('XXX_USER_PROFILE_OFFERS_TITLE'));
        $daoOffer = new Application_Model_DAO_Offer();
        $list = $daoOffer->getOffersByAuthor($this->userId)->query()->fetchAll();
        $this->view->list = $this->createPaginator($list);
        $this->render('profile');
    }
    
    public function userCatalogsAction(){
        $this->userLoginRequired();
        $daoCatalog = new Application_Model_DAO_Catalog();
        $list = $daoCatalog->getCatalogsByAuthor($this->userId)->query()->fetchAll();
        $this->view->list = $this->createPaginator($list);
        $this->render('profile');
    }

    public function logoutAction() {
        $this->setCurrentUser();
        Zend_Auth::getInstance()->clearIdentity();
        $session = new Zend_Session_Namespace('requestURL');
        $session->unsetAll();
        // todo delete cookies
        $this->addSuccess($this->_('XXX_USER_LOGOUT_SUCCESS'));
        $url = $this->url(array('controller' => 'index', 'action' => 'index'));
        $this->_redirect($url);
    }

    public function registerAction() {
        $this->setContentTitle(_('XXX_USER_REGISTER_PAGE_TITLE'));

        $form = new Application_Form_User_Register();

        if (!$this->isPost() || !$form->isValid($_POST)) {
            $this->view->form = $form;
            return;
        }
        $data = $form->getValues();

        $daoUser = new Application_Model_DAO_User();
        $user = $daoUser->register($data);

        $userObj = new Application_Model_UserBean();
        $userObj->user_id = $user['user_id'];
        $userObj->email = $user['email'];
        Zend_Auth::getInstance()->getStorage()->write($userObj);

        $this->setCurrentUser($user);

        $this->addSuccess($this->_('XXX_USER_REGISTER_SUCCESS'));

        $url = $this->url(array('controller' => 'user', 'action' => 'profile'));
        $this->_redirect($url);
    }

}

